package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.ScriptStepResponse;
import com.baihan.angus.app.game.common.constants.CommonVariable;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class QuestionOptionAdp extends RecyclerView.Adapter {

    private Context mContext;
    private List<ScriptStepResponse.DataX.Question.Data> mQuestionList;
    private RecyclerView mRecyclerView;

    private OnItemClickListener mOnItemClickListener;

    private Disposable mDisposable;
    public long timeLimit = 60;

    public QuestionOptionAdp(Context mContext, List<ScriptStepResponse.DataX.Question.Data> mQuestionList) {
        this.mContext = mContext;
        this.mQuestionList = mQuestionList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        ScriptStepResponse.DataX.Question.Data questionItem = mQuestionList.get(position);

        vh.tvQuestion.setText(questionItem.getTitle());

        RecyclerView rvOptions = vh.rvOptions;
        rvOptions.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        QuestionOptionItemAdp mQuestionOptionItemAdp = new QuestionOptionItemAdp(mContext, questionItem.getOptions(), questionItem.getCategory());
        rvOptions.setAdapter(mQuestionOptionItemAdp);
        mQuestionOptionItemAdp.bindRecycleView(rvOptions);

        if (position == 0) {
            vh.tvPrevious.setVisibility(View.GONE);
            if (mQuestionList.size() == 1) {
                vh.tvNext.setText(mContext.getString(R.string.determine));
            } else {
                vh.tvNext.setText(mContext.getString(R.string.nextQuestion));
            }
        } else if (position == mQuestionList.size() - 1) {
            vh.tvPrevious.setVisibility(View.VISIBLE);
            vh.tvNext.setText(mContext.getString(R.string.determine));
        } else {
            vh.tvPrevious.setVisibility(View.VISIBLE);
            vh.tvNext.setText(mContext.getString(R.string.nextQuestion));
        }

//        timerStart(vh.tvTimeLimit);

        if (questionItem.getCategory() == 0) {
            vh.tvType.setText("(投票单选)");
        } else {
            vh.tvType.setText("(投票多选)");
        }

        vh.tvPrevious.setOnClickListener(v -> {
            mOnItemClickListener.onPrevious(position);
        });

        vh.tvNext.setOnClickListener(v -> {
            if (vh.tvNext.getText().equals(mContext.getString(R.string.nextQuestion))) {
                mOnItemClickListener.onNext(position);
            } else if (vh.tvNext.getText().equals(mContext.getString(R.string.determine))) {
                mOnItemClickListener.onSubmit();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mQuestionList.size();
    }

    /**
     * 绑定recycleview
     *
     * @param recyclerView
     */
    public void bindRecycleView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        recyclerView.setAdapter(this);
    }

    /**
     * 更新信息
     */
    public void updateQuestionList(List<ScriptStepResponse.DataX.Question.Data> scripts) {
        mQuestionList.clear();
        mQuestionList.addAll(scripts);
        if (mRecyclerView != null) {
            notifyDataSetChanged();
        }
    }

    /**
     * 倒计时
     */
    public void timerStart(TextView tv) {
        Observable.interval(0, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                        if (timeLimit == 60) {
                            CommonVariable.REGISTER_TIME = System.currentTimeMillis() + 60 * 1000;
                        }
                    }

                    @Override
                    public void onNext(Long aLong) {
                        long a = timeLimit - aLong;
                        if (a > 0) {
                            tv.setText("倒计时：" + a + "s");
                        } else {
                            stopDispose();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        stopDispose();
                    }

                    @Override
                    public void onComplete() {
                        stopDispose();
                    }
                });

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvQuestion, tvType, tvTimeLimit, tvPrevious, tvNext;
        RecyclerView rvOptions;
        LinearLayout llItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvQuestion = itemView.findViewById(R.id.tvQuestion);
            tvType = itemView.findViewById(R.id.tvType);
            tvTimeLimit = itemView.findViewById(R.id.tvTimeLimit);
            tvPrevious = itemView.findViewById(R.id.tvPrevious);
            tvNext = itemView.findViewById(R.id.tvNext);
            rvOptions = itemView.findViewById(R.id.rvOptions);
            llItem = itemView.findViewById(R.id.llItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onPrevious(int position);

        void onNext(int position);

        void onSubmit();
    }

    private void stopDispose() {
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }
}
